package com.ruoyi.project.cms.role.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.role.domain.SysRole;
import com.ruoyi.project.cms.role.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 公司:
 * 作者:胖虎
 * 类名:SysRoleController
 * 描述:
 * 时间:2020-05-19
 */
@Controller
@RequestMapping("/cms/role")
public class SysRoleController   extends BaseController {

    private String prefix = "cms/role";

     @Autowired
    private SysRoleService  roleService;

    /**
     * 角色页面的跳转
     * @return
     */
    @GetMapping()
    public String role()
    {
        return prefix + "/role";
    }

    /**
     * 角色表格数据查询
     * @param role
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public  TableDataInfo  list(SysRole role)
    {
        System.out.println("我是role"+role);
        startPage();
        List<SysRole> list = roleService.selectRoleList(role);
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 新增角色的跳转
     * @return
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 校验角色名称
     */
    @PostMapping("/checkRoleNameUnique")
    @ResponseBody
    public String checkRoleNameUnique(SysRole role)
    {
        return  roleService.checkRoleNameUnique(role);
    }


    /**
     * 新增保存角色
     */

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave( SysRole role)
    {
        if (UserConstants.ROLE_NAME_NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
        {
            return error("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }

        return toAjax(roleService.insertRole(role));

    }


    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult  changeStatus(SysRole role)
    {

        return toAjax(roleService.changeStatus(role));
    }

    /**
     * 删除橘色
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String  ids)
    {
        try
        {
            return  toAjax(roleService.deleteRoleByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }


}
